<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Konfigurasi_model extends CI_Model {

	function __construct()
	{
		parent::__construct();
		$this->CI = get_instance();
	}
	
	function insert_konfigurasi($data){
		$query = $this->db->insert('konfigurasi',$data);
		return $query;
	}
	
	function getId(){
		//Select table column
		$this->db->select('count(*) as jml, ID_KONFIGURASI');
		$this->db->from('konfigurasi'); 
		$query = $this->db->get();
		return $query;
	}
	
	function get_konfigurasiID($id){
		//Select table column
		$this->db->select('*');
		$this->db->from('konfigurasi'); 
		$this->db->where('ID_KONFIGURASI', $id);
		$query = $this->db->get();
		return $query;
	}
	
	function update_konfigurasi($data,$id){
		$this->db->where('ID_KONFIGURASI',$id);
		$this->db->update('konfigurasi',$data);
	}
	
	function get_logo(){
		$this->db->select('FOTO, NAMA_SEKOLAH, ALAMAT_SEKOLAH_1');
		$this->db->from('tb_master_sekolah'); 
		$query = $this->db->get();
		return $query;
	}
	
	function get_header(){
		$this->db->select('*');
		$this->db->from('konfigurasi'); 
		$query = $this->db->get();
		return $query;
	}
	
	function get_litsmateri(){
		$this->db->select('*');
		$this->db->from('tb_upload_materi a');
		$this->db->join('tb_pelajaran b','b.KODE_MATA_PELAJARAN=a.ID_PELAJARAN'); 
		$this->db->where('b.TAHUN_AJARAN',$this->session->userdata('tahunajaran'));
		$this->CI->flexigrid->build_query();
		$return['records'] = $this->db->get();
		
		$this->db->select('*');
		$this->db->from('tb_upload_materi a');
		$this->db->join('tb_pelajaran b','b.KODE_MATA_PELAJARAN=a.ID_PELAJARAN');
		$this->db->where('b.TAHUN_AJARAN',$this->session->userdata('tahunajaran'));
		$this->CI->flexigrid->build_query(FALSE);
		$return['record_count'] = $this->db->count_all_results();

		return $return;
	}
	
	function delete_materi($id){
		$this->db->where('ID_MATERI', $id);
		$this->db->delete('tb_upload_materi'); 
	}
	
	
	
	function update_materi($data,$id){
		$this->db->where('ID_MATERI',$id);
		$this->db->update('tb_upload_materi',$data);
	}
	
	function get_lastid(){
		$this->db->select('max(ID_MATERI) as lastid');
		$this->db->from('tb_upload_materi'); 
		$query = $this->db->get();
		return $query;
	}
	
	function get_jml($id){
		$this->db->select('count(*) as jml_download');
		$this->db->from('tb_history_dmateri'); 
		$this->db->where('ID_MATERI',$id);
		$query = $this->db->get();
		return $query;
	}
	
	function insert_history_materi($data){
		$query = $this->db->insert('tb_history_dmateri',$data);
		return $query;
	}
	
	function get_litsdownloader($id_materi){
		$this->db->select('*');
		$this->db->from('tb_history_dmateri a');
		$this->db->join('users b','b.ID_USER=a.DOWNLOADER'); 
		$this->db->where('a.ID_MATERI',$id_materi);
		$this->CI->flexigrid->build_query();
		$return['records'] = $this->db->get();
		
		$this->db->select('*');
		$this->db->from('tb_history_dmateri a');
		$this->db->join('users b','b.ID_USER=a.DOWNLOADER'); 
		$this->db->where('a.ID_MATERI',$id_materi);
		$this->CI->flexigrid->build_query(FALSE);
		$return['record_count'] = $this->db->count_all_results();

		return $return;
	}

	// ==============================================
	
	function insert_surat($data){
		$query = $this->db->insert('tb_upload_surat',$data);
		return $query;
	}
	
	function insert_history_surat($data){
		$query = $this->db->insert('tb_history_dsurat',$data);
		return $query;
	}
	
	function get_listsurat(){
		$this->db->select('*');
		$this->db->from('tb_upload_surat');
		$this->CI->flexigrid->build_query();
		$return['records'] = $this->db->get();
		
		$this->db->select('*');
		$this->db->from('tb_upload_surat');
		$this->CI->flexigrid->build_query(FALSE);
		$return['record_count'] = $this->db->count_all_results();

		return $return;
	}
	
	function delete_surat($id){
		$this->db->where('ID_SURAT', $id);
		$this->db->delete('tb_upload_surat'); 
	}
	
	function get_suratID($id){
		
		//Select table column
		$this->db->select('*');
		$this->db->from('tb_upload_surat a'); 
		$this->db->where('ID_SURAT',$id);
		$query = $this->db->get();
		return $query;
	}
	
	function update_surat($data,$id){
		$this->db->where('ID_SURAT',$id);
		$this->db->update('tb_upload_surat',$data);
	}
	
	function get_lastid_surat(){
		$this->db->select('max(ID_SURAT) as lastid');
		$this->db->from('tb_upload_surat'); 
		$query = $this->db->get();
		return $query;
	}
}

?>
